//
//  XMGScheduleCell.m
//  码哥课堂
//
//  Created by yz on 2016/12/3.
//  Copyright © 2016年 yz. All rights reserved.
//

#import "XMGScheduleCell.h"
#import "XMGDayItem.h"
#import "XMGPlanView.h"
#import "UIColor+Color.h"
#import "XMGCoursePlanView.h"

@interface XMGScheduleCell ()

@property (nonatomic, strong) XMGCoursePlanView *coursePlaneView;

@end

@implementation XMGScheduleCell

- (XMGCoursePlanView *)coursePlaneView
{
    if (_coursePlaneView == nil) {
        XMGCoursePlanView *coursePlaneView = [XMGCoursePlanView coursePlaneView];
        _coursePlaneView = coursePlaneView;
    }
    return _coursePlaneView;
}

- (void)setDays:(NSArray *)days
{
    _days = days;
    
    self.coursePlaneView.center = CGPointMake(self.width * 0.5, self.height * 0.3);
    [self.contentView addSubview:self.coursePlaneView];
    
    // 显示一周的数据
    for (XMGDayItem *item in days) {
//        NSLog(@"%@",item);
        [self setUpOneDay:item];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 调整子控件的位置
    
}

// 设置一天的数据
- (void)setUpOneDay:(XMGDayItem *)item
{
    
    // 创建当天View
    NSInteger row = item.day;
    NSInteger cols = 3;
    CGFloat dayH = (XMGScreenHeight - 64 - 49 - 40 - 42) / 7;
    CGFloat margin = 10;
    CGFloat dayW = XMGScreenWidth - 100;
    CGFloat dayY = row * dayH;
    UIView *dayView = [[UIView alloc] initWithFrame:CGRectMake(0, dayY, dayW, dayH)];
    
    [self.contentView addSubview:dayView];
    
    CGFloat planW = (self.width - (cols - 1) * margin) / cols;
    CGFloat planH = dayH - (2 * margin);
    CGFloat planX = 0;
    CGFloat planCenterY = dayH * 0.5;
    if (item.am) {
        XMGPlanView *planView = [XMGPlanView planView];
        planView.item = item.am;
        planView.frame = CGRectMake(planX, 0, planW, planH);
        planView.centerY = planCenterY;
        [dayView addSubview:planView];
        
        [self.coursePlaneView removeFromSuperview];
    }
    
    if (item.pm) {
        // 居中
        XMGPlanView *planView = [XMGPlanView planView];
        planView.item = item.pm;
        planView.frame = CGRectMake(planX, 0, planW, planH);
        planView.centerX = dayW * 0.5;
        planView.centerY = planCenterY;
        [dayView addSubview:planView];
        
        [self.coursePlaneView removeFromSuperview];
    }
    
    if (item.night) {
        planX = dayW - planW;
        XMGPlanView *planView = [XMGPlanView planView];
        planView.item = item.night;
        planView.frame = CGRectMake(planX, 0, planW, planH);
        planView.centerY = planCenterY;
        [dayView addSubview:planView];
        
        [self.coursePlaneView removeFromSuperview];
    }
    
    
}

@end
